<template>
  <div class="consult-illness-page">
    <nav-bar title="图文问诊" />
    <div class="illness-tip van-hairline--bottom">
      <img class="img" src="@/assets/images/avatar-doctor.svg" alt="" />
      <div class="info">
        <p class="tit">在线医生</p>
        <p class="tip">
          请描述你的疾病或症状、是否用药、就诊经历，需要我听过什么样的帮助
        </p>
        <p class="safe">
          <icon name="consult-safe" />
          <span>内容仅医生可见</span>
        </p>
      </div>
    </div>
    <div class="illness-form">
      <van-field
        v-model="content"
        rows="3"
        autosize
        type="textarea"
        placeholder="请详细描述您的病情，病情描述不能为空"
      />
      <div class="item">
        <p>本次患病多久了？</p>
        <radio-button :options="timeOptions" v-model="time" />
      </div>
      <div class="item">
        <p>此次病情是否去医院就诊过？</p>
        <radio-button :options="flagOptions" v-model="flag" />
      </div>
      <div class="illness-img">
        <van-uploader
          v-model="fileList"
          upload-icon="photo-o"
          upload-text="上传图片"
          :after-read="afterRead"
          :max-count="9"
          @delete="deletePic"
        >
        </van-uploader>
        <p v-if="fileList.length === 0" class="tip">
          上传内容仅医生可见,最多9张图,最大5MB
        </p>
      </div>
      <van-button round block type="primary" :class="{ disabled }" @click="next"
        >下一步</van-button
      >
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Toast } from 'vant'
import { ref, computed } from 'vue'
import { ApiRes } from '@/types/data'
import request from '@/utils/request'
import { useRouter } from 'vue-router'
import useStore from '@/store'
const { consult } = useStore()
const content = ref('')
const router = useRouter()
const time = ref()
const flag = ref()
const timeOptions = [
  { label: '一周内', value: 1 },
  { label: '一月内', value: 2 },
  { label: '半年内', value: 3 },
  { label: '大于半年', value: 4 }
]
const flagOptions = [
  { label: '就诊过', value: 0 },
  { label: '没就诊过', value: 1 }
]
// 文件上传相关
const fileList = ref([])
let pictures = [] as { id: string; url: string }[]
// 上传文件
const afterRead = async (file: any) => {
  const formData = new FormData()
  formData.append('file', file.file)
  const res = await request.post<ApiRes<{ id: string; url: string }>>(
    'upload',
    formData
  )

  // 设置一下id，方便删除
  file.id = res.data.data.id
  pictures.push(res.data.data)
}
// 删除文件
const deletePic = (file: any) => {
  pictures = pictures.filter(item => item.id !== file.id)
}
// 下一步按钮是否可用（样式控制）
const disabled = computed(() => {
  return (
    content.value.trim().length === 0 || !time.value || flag.value === undefined
  )
})
// 下一步
const next = () => {
  if (content.value.trim().length === 0) {
    return Toast({ message: '请输入病情描述', duration: 1000 })
  }
  if (!time.value) {
    return Toast({ message: '请选择症状持续时间', duration: 1000 })
  }
  if (flag.value === undefined) {
    return Toast({ message: '请选择是否已经就诊', duration: 1000 })
  }

  // 保存图文问诊信息到仓库
  consult.setIllness({
    illnessDesc: content.value,
    illnessTime: time.value,
    consultFlag: flag.value,
    pictures: pictures
  })

  // 跳转到选择患者页面
  router.push(`/my/patient?isSelect=true`)
}
</script>

<style lang="scss" scoped>
.consult-illness-page {
  padding-top: 46px;
}
.illness-tip {
  display: flex;
  padding: 15px;
  .img {
    width: 52px;
    height: 52px;
    border-radius: 4px;
    overflow: hidden;
    margin-top: 10px;
  }
  .info {
    flex: 1;
    padding-left: 12px;
    .tit {
      font-size: 16px;
      margin-bottom: 5px;
    }
    .tip {
      padding: 12px;
      background: var(--cp-bg);
      color: var(--cp-text3);
      font-size: 13px;
      margin-bottom: 10px;
    }
    .safe {
      font-size: 10px;
      color: var(--cp-text3);
      display: flex;
      align-items: center;
      .cp-icon {
        font-size: 12px;
        margin-right: 2px;
      }
    }
  }
}
.illness-form {
  padding: 15px;
  .van-field {
    padding: 0;
    &::after {
      border-bottom: none;
    }
  }
  .item {
    > p {
      color: var(--cp-text3);
      padding: 15px 0;
    }
  }
}
.illness-img {
  padding-top: 16px;
  margin-bottom: 40px;
  display: flex;
  align-items: center;
  .tip {
    font-size: 12px;
    color: var(--cp-tip);
  }
  ::v-deep() {
    .van-uploader {
      &__preview {
        &-delete {
          left: -6px;
          top: -6px;
          border-radius: 50%;
          background-color: var(--cp-primary);
          width: 20px;
          height: 20px;
          &-icon {
            transform: scale(0.9) translate(-22%, 22%);
          }
        }
        &-image {
          border-radius: 8px;
          overflow: hidden;
        }
      }
      &__upload {
        border-radius: 8px;
      }
      &__upload-icon {
        color: var(--cp-text3);
      }
    }
  }
}
.van-button {
  font-size: 16px;
  margin-bottom: 30px;
  &.disabled {
    opacity: 1;
    background: #fafafa;
    color: #d9dbde;
    border: #fafafa;
  }
}
</style>
